﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Service
{
    public partial class UserFavoritePagedList : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userid = Request["userid"] ?? "";
            int pagesize = 0;
            int pageindex = 0;
            try
            {
                pagesize = int.Parse(Request["pagesize"]??"0");
                pageindex = int.Parse(Request["pageindex"]??"0");
            }
            catch
            {
                Response.Write("113");//无效的整型数值
                return;
            }
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, userid, pagesize,pageindex))
            {
                Response.Write("112");//密钥不正确 
                return;
            }


            if (Request["userid"] != null && Request["pagesize"] != null && Request["pageindex"] != null)
            {

                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user != null)
                {
                    Model.NetPager pager = new Model.NetPager();
                    pager.TableName = "Favorite";
                    pager.PrimaryKey = "FavoriteID";
                    pager.PageSize = pagesize;
                    pager.PageIndex = pageindex;
                    pager.Order = "CreateDate desc,FavoriteID asc";
                    pager.SortType = (int)Common.Enum.SortType.Multi;
                    pager.Where = "UserID='" + user.UserID + "'";
                    IList<Model.ServiceInfo> list = BLL.FavoriteBLL.Instance.GetPagedFavoriteServiceList(pager);
                    Hashtable ht = new Hashtable();
                    ht.Add("RecordCount", pager.TotalCount);
                    ht.Add("ServiceList", list);
                    Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));//成功
                }
                else
                {
                    Response.Write("301");//无效的用户
                    return;
                }
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string userid, int pagesize, int pageindex)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey =base.EncryptMD5(public_key + current_day + userid + pagesize.ToString() + pageindex.ToString()).Substring(6,22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}